using UnityEngine;
using QFramework;
using System.Linq;



namespace ProjectSurvivor
{
	public partial class SimpleSword : ViewController//表示一个挂在场景物体上的控制器
	{
		private float mCurrentSeconds = 0;
		public BindableProperty<bool> SupperSimpleSword = new BindableProperty<bool>(false);
		private void Update()
		{
			mCurrentSeconds += Time.deltaTime;
			if (mCurrentSeconds >= Global.SimpleAbilityDuration.Value)
			{
				var countTimes = SupperSimpleSword.Value ? 2 : 1;
				var damageTimes = SupperSimpleSword.Value ? Random.Range(2, 3 + 1) : 1;
				var distanceTimes = SupperSimpleSword.Value ? 2 : 1;
				mCurrentSeconds = 0;
				var enemies = FindObjectsByType<Enemy>(FindObjectsInactive.Exclude, FindObjectsSortMode.None);//调用 Unity（2021+ 版本）的 FindObjectsByType 方法，查找场景中所有激活的 Enemy 类型对象（FindObjectsInactive.Exclude 排除未激活物体 ），存入 enemies 集合，用于后续遍历检测。
				foreach (var enemy in enemies.OrderBy(e => e.Direction2DFrom(Player.Default).magnitude).Take(Global.SimpleSwordCount.Value*countTimes).Where(e => e.Direction2DFrom(Player.Default).magnitude < Global.SimpleSwordRange.Value*damageTimes))
				{
					if (Global.SimpleSwordCount.Value > 10)
					{
						SupperSimpleSword.Value = true;
					}
					Sword.Instantiate()
					.Position(enemy.Position() + Vector3.left * 0.25f).Show()
					.Self(self =>
					{
						var selfCache = self;
						selfCache.OnTriggerEnter2DEvent(collider2D =>
						{
							var hurtBox = collider2D.GetComponent<HurtBox>();
							if (hurtBox)
							{
								if (hurtBox.Owner.CompareTag("Enemy"))
								{
									var damageTimes = SupperSimpleSword.Value ? Random.Range(2, 3 + 1) :1;
											if (hurtBox.Owner.GetComponent<Enemy>())
											{
												DamageSystem.CalculateDamage(Global.SimpleAbilityDamage.Value*damageTimes, hurtBox.Owner.GetComponent<Enemy>());
											}
											else
											{
												DamageSystem.CalculateDamage(Global.SimpleAbilityDamage.Value*damageTimes, hurtBox.Owner.GetComponent<EnemyMinBoss>());
											}
								}
							}
						}).UnRegisterWhenGameObjectDestroyed(gameObject);
						ActionKit
						.Sequence()
						.Callback(() =>
						{
							selfCache.enabled = false;
						})
						.Parallel(p =>
						{
							p.Lerp(0, 10, 0.2f, z => selfCache.LocalEulerAnglesZ(z));
							p.Append
							(
							ActionKit.Sequence()
							.Lerp(0, 1.25f, 0.1f, scale => selfCache.LocalScale(scale))
							.Lerp(1.25f, 1, 0.1f, scale => selfCache.LocalScale(scale))
							);
						})
						.Callback(() =>
						{
							selfCache.enabled = true;
						})
						.Parallel(p =>
						{
							p.Lerp(10, -180, 0.2f, z => selfCache.LocalEulerAnglesZ(z));
							p.Append
							(
							ActionKit.Sequence()
							.Lerp(0, 1.25f, 0.1f, scale => selfCache.LocalScale(scale))
							.Lerp(1.25f, 1, 0.1f, scale => selfCache.LocalScale(scale))
							);
						})
						.Callback(() =>
						{
							selfCache.enabled = false;
						})
						.Lerp(-180, 0, 0.3f, z =>
						{
							selfCache.LocalEulerAnglesZ(z)
							.LocalScale(z.Abs() / 180);

						})
						.Start(this, () =>
						{
							selfCache.DestroyGameObjGracefully();
						});
					});

				}
			}
		}
		}
	}

